package com.epam.diplom.entity;

import java.io.IOException;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("request")
public class SpringLoginBean {
	Logger logger = LoggerFactory.getLogger(SpringLoginBean.class);
	private String user;
	private String password;
	private boolean storeUser = false;
	private boolean logIn = false;

	public String getUser() {
		return this.user;
	}

	public void setUser(final String user) {
		this.user = user;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

	public boolean isStoreUser() {
		return this.storeUser;
	}

	public void setStoreUser(final boolean storeUser) {
		this.storeUser = storeUser;
	}

	public boolean isLogIn() {
		return this.logIn;
	}

	public void setLogIn(final boolean logIn) {
		this.logIn = logIn;
	}

	public String loginAction() throws IOException, ServletException {
		logger.info("sfasfasfasfas");
		ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
		RequestDispatcher dispatcher = ((ServletRequest) context.getRequest())
				.getRequestDispatcher("/j_spring_security_check");
		dispatcher.forward((ServletRequest) context.getRequest(),
				(ServletResponse) context.getResponse());
		FacesContext.getCurrentInstance().responseComplete();
		return "ok";
	}
}
